﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimDuong
{
    public partial class Form1 : Form
    {
        public const int MAX_X = 10;
        public const int MAX_Y = 10;

        Panel[,] panels = new Panel[10, 10];        
        int[,] matran = new int[10, 10];
        
        BFS bfs;

        public Form1()
        {
            InitializeComponent();
            int _x = 200;
            int _y = 20;            
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    matran[i, j] = 0;
                    panels[i, j] = createPanel(new System.Drawing.Point(_x, _y));
                    this.Controls.Add(panels[i, j]);
                    _x += 35;
                }
                _x = 200;
                _y += 35;
            }
            butTim.Enabled = false;
        }

        /// <summary>
        /// Khôi phục mặt định
        /// </summary>
        private void ResetField()
        {
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 10; j++)
                {
                    matran[i, j] = 0;
                    panels[i, j].BackgroundImage = null;
                    panels[i, j].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
                }            
        }

        private Panel createPanel(System.Drawing.Point location)
        {
            Panel panelX = new Panel();
            panelX.Location = location;
            panelX.Size = new System.Drawing.Size(35, 35);
            panelX.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            panelX.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            panelX.BackgroundImageLayout = ImageLayout.Stretch;
            return panelX;
        }

        private void createMap(int agent_x, int agent_y, int target_x, int target_y)
        {   
            matran[agent_x, agent_y] = 1;
            panels[agent_x, agent_y].BackgroundImage = global::TimDuong.Properties.Resources.agent_normal;

            matran[target_x, target_y] = 2;
            panels[target_x, target_y].BackgroundImage = global::TimDuong.Properties.Resources.Gold;

            Random random = new Random();
            int x, y;
            for (int i = 0; i < 30; i++)
            {
                x = random.Next(10);
                y = random.Next(10);
                if (matran[x, y] == 0)
                {
                    matran[x, y] = 1;
                    panels[x, y].BackgroundImage = global::TimDuong.Properties.Resources.pit;
                }
                else
                    i--;
            }
        }        

        /// <summary>
        /// Hiện đường đi lên bản đồ
        /// </summary>
        private void RawPath()
        {
            for (int i = 0; i < bfs.Path_index; i++)
            {
                panels[bfs.Path[i, 0], bfs.Path[i, 1]].BackColor = System.Drawing.Color.Yellow;                
            }
        }

        private void butCreate_Click(object sender, EventArgs e)
        {
            ResetField();
            if (int.Parse(txtAgent_X.Text) > 10 || int.Parse(txtAgent_X.Text) < 1 ||
                int.Parse(txtAgent_Y.Text) > 10 || int.Parse(txtAgent_Y.Text) < 1 ||
                int.Parse(txtTarget_X.Text) > 10 || int.Parse(txtTarget_X.Text) < 1 ||
                int.Parse(txtTarget_Y.Text) > 10 || int.Parse(txtTarget_Y.Text) < 1)
            {
                MessageBox.Show("Tọa độ không chính xác nhập lại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                createMap(int.Parse(txtAgent_X.Text) - 1,
                          int.Parse(txtAgent_Y.Text) - 1,
                          int.Parse(txtTarget_X.Text) - 1,
                          int.Parse(txtTarget_Y.Text) - 1);
                bfs = new BFS(matran, int.Parse(txtAgent_X.Text) - 1, int.Parse(txtAgent_Y.Text) - 1);
                butTim.Enabled = true;
            }
        }

        private void butTim_Click(object sender, EventArgs e)
        {            
            if (bfs.FindPath())
            {
                RawPath();
                butTim.Enabled = false;
            }
            else
                MessageBox.Show("Không tìm thấy đường đi", "Thông báo", MessageBoxButtons.OK);
        }

        private void butAutoValue_Click(object sender, EventArgs e)
        {
            Random random = new Random();
            txtAgent_X.Text = random.Next(1, 10).ToString();
            txtAgent_Y.Text = random.Next(1, 10).ToString();

            int x, y;
            do 
            {
                x = random.Next(1, 10);
                y = random.Next(1, 10);                
            } while (x.ToString() == txtAgent_X.Text && y.ToString() == txtAgent_Y.Text);
            txtTarget_X.Text = x.ToString();
            txtTarget_Y.Text = y.ToString();
            
        }        
               
    }
}
